package com.ruoyi.blindBoxManage.controller;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.blindBoxManage.domain.BlindBox;
import com.ruoyi.blindBoxManage.mapper.ClassMapper;
import com.ruoyi.blindBoxManage.mapper.PlayTypeMapper;
import com.ruoyi.blindBoxManage.service.IBlindBoxService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 盲盒配置Controller
 *
 * @author ruoyi
 * @date 2025-08-12
 */
@RestController
@RequestMapping("/blindBox/blindBox")
public class BlindBoxController extends BaseController {
    @Autowired
    private IBlindBoxService blindBoxService;
    @Resource
    private ClassMapper classMapper;
    @Resource
    private PlayTypeMapper playTypeMapper;

    /**
     * 查询盲盒配置列表
     */
    @PreAuthorize("@ss.hasPermi('blindBox:blindBox:list')")
    @GetMapping("/list")
    public TableDataInfo list(BlindBox blindBox) {
        if (!StrUtil.isEmpty(blindBox.getClassName())) {
            Integer classId = classMapper.selectIdByName(blindBox.getClassName());
            if (classId != null) {
                blindBox.setClassId(classId);
            } else {
                return getDataTable(Collections.emptyList());
            }
        }
        if (!StrUtil.isEmpty(blindBox.getPlayTypeName())) {
            Integer playTypeId = playTypeMapper.selectIdByName(blindBox.getPlayTypeName());
            if (playTypeId != null) {
                blindBox.setPlayTypeId(playTypeId);
            } else {
                return getDataTable(Collections.emptyList());
            }
        }
        startPage();
        List<BlindBox> list = blindBoxService.selectBlindBoxList(blindBox);
        if (!CollectionUtils.isEmpty(list)) {
            for (BlindBox box : list) {
                String className = classMapper.selectNameById(box.getClassId());
                box.setClassName(!StrUtil.isEmpty(className) ? className : "");
                String playTypeName = playTypeMapper.selectNameById(box.getPlayTypeId());
                box.setPlayTypeName(!StrUtil.isEmpty(playTypeName) ? playTypeName : "");
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出盲盒配置列表
     */
    @PreAuthorize("@ss.hasPermi('blindBox:blindBox:export')")
    @Log(title = "盲盒配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BlindBox blindBox) {
        if (!StrUtil.isEmpty(blindBox.getClassName())) {
            Integer classId = classMapper.selectIdByName(blindBox.getClassName());
            if (classId != null) {
                blindBox.setClassId(classId);
            }
        }
        if (!StrUtil.isEmpty(blindBox.getPlayTypeName())) {
            Integer playTypeId = playTypeMapper.selectIdByName(blindBox.getPlayTypeName());
            if (playTypeId != null) {
                blindBox.setPlayTypeId(playTypeId);
            }
        }
        List<BlindBox> list = blindBoxService.selectBlindBoxList(blindBox);
        if (!CollectionUtils.isEmpty(list)) {
            for (BlindBox box : list) {
                String className = classMapper.selectNameById(box.getClassId());
                box.setClassName(!StrUtil.isEmpty(className) ? className : "");
                String playTypeName = playTypeMapper.selectNameById(box.getPlayTypeId());
                box.setPlayTypeName(!StrUtil.isEmpty(playTypeName) ? playTypeName : "");
            }
        }
        ExcelUtil<BlindBox> util = new ExcelUtil<BlindBox>(BlindBox.class);
        util.exportExcel(response, list, "盲盒配置数据");
    }

    /**
     * 获取盲盒配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('blindBox:blindBox:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        BlindBox blindBox = blindBoxService.selectBlindBoxById(id);
        if (!ObjectUtils.isEmpty(blindBox)) {
            String className = classMapper.selectNameById(blindBox.getClassId());
            blindBox.setClassName(!StrUtil.isEmpty(className) ? className : "");
            String playTypeName = playTypeMapper.selectNameById(blindBox.getPlayTypeId());
            blindBox.setPlayTypeName(!StrUtil.isEmpty(playTypeName) ? playTypeName : "");

        }

        return success(blindBox);
    }

    /**
     * 新增盲盒配置
     */
    @PreAuthorize("@ss.hasPermi('blindBox:blindBox:add')")
    @Log(title = "盲盒配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BlindBox blindBox) {
        return toAjax(blindBoxService.insertBlindBox(blindBox));
    }


    /**
     * 修改盲盒配置
     */
    @PreAuthorize("@ss.hasPermi('blindBox:blindBox:edit')")
    @Log(title = "盲盒配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BlindBox blindBox) {
        return toAjax(blindBoxService.updateBlindBox(blindBox));
    }

    /**
     * 删除盲盒配置
     */
    @PreAuthorize("@ss.hasPermi('blindBox:blindBox:remove')")
    @Log(title = "盲盒配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(blindBoxService.deleteBlindBoxByIds(ids));
    }
}
